﻿#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force
#Persistent
#Include json.ahk
#Include convertcodepage.ahk
#Include URLDownloadToVar.ahk
OnMessage(0x218, "PowerBroadcast")
Menu, Tray, NoStandard
Menu, Tray, Add, 立即更新, mainapp
Menu, Tray, Add, 退出, exits
alld :=
IniRead, ccode, config.ini, locale, ccode, 101010100
IniRead, cname, config.ini, locale, cname, 北京
mainapp:           ;更新气泡
gosub, prechecknet        ;去检查网络连接
gosub, maindata          ;数据获取
gosub, tts         ;显示托盘气泡
gosub, bdwik   ;创建星期数组
Loop   ;用来更新托盘图标和鼠标悬停提示
{
	gosub, prechecknet     ;去检查网络连接
	gosub, maindata
	gosub, wikd    ;获取大后天的"星期"
	gosub, iconss    ;控制托盘图标
	alld := "今天: " . 1weather1 . "  " . 1temp1 . "`n明天: " . 1weather2 . "  " . 1temp2 . "`n后天: "1weather3 . "  " . 1temp3 . "`n" . tday2 . ": " . 1weather4 . "  " . 1temp4 . "`n`n更新时间: " . 2time
	Menu, Tray, Tip, %alld%
	Sleep, 1200000
}
return

maindata:           ;数据获取
U1 := "http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=" . ccode . "&weatherType=0"
U2 := "http://www.weather.com.cn/data/sk/" . ccode . ".html"
U1 := URLDownloadToVar(U1)
UA1 := UTF82Ansi(U1) . "}}"              ;转码,UTF82Ansi 函数
1weather1 := json(UA1, "weatherinfo.weather1")          ;详细天气预报
1weather2 := json(UA1, "weatherinfo.weather2")
1weather3 := json(UA1, "weatherinfo.weather3")
1weather4 := json(UA1, "weatherinfo.weather4")
1temp1 := json(UA1, "weatherinfo.temp1")               ;今日
1temp2 := json(UA1, "weatherinfo.temp2")               ;明日
1temp3 := json(UA1, "weatherinfo.temp3")               ;后天
1temp4 := json(UA1, "weatherinfo.temp4")               ;大后天
1wind1 := json(UA1, "weatherinfo.wind1")               ;风
U2 := URLDownloadToVar(U2)
UA2 := UTF82Ansi(U2) . "}}"              ;用 URLDownloadToVar 下载下来少了最后 2 个大括号, 手动补上
2temp := json(UA2, "weatherinfo.temp")         ;实时天气预报
2time := json(UA2, "weatherinfo.time")   ;更新时间
return

tts:
TrayTip, %cname%今天天气, `n现在温度: %2temp%°C`n天气: %1weather1%`n最高最低温度: %1temp1%`n风向: %1wind1%, 4, 0
return

wikd:
tday := A_WDay
tday := tday + 3
tday := Mod(tday, 7)    ;使之不大于7
tday2 :=weekday[tday]      ;获取“后天”的星期
return

bdwik:
weekday := Object()
weekday.Insert("周日")
weekday.Insert("周一")
weekday.Insert("周二")
weekday.Insert("周三")
weekday.Insert("周四")
weekday.Insert("周五")
weekday.Insert("周六")
return

prechecknet:          ;检查网络
Loop
{
	If ConnectedToInternet()
	{
		break
		return
	}
	else
	{
		if (A_Index < 11 )
		{
			st := A_Index * 1000
			alldd := alld
			alls := alldd . "`n" . A_Index . "秒后重连..."
			Menu, Tray, Tip, %alls%
			Sleep, %st%
		}
		else
		{
			alldd := alld
			alls := alldd . "`n" . "2分钟后重连..."
			Menu, Tray, Tip, %alls%
			Sleep, %120000%
		}
	}
}
Return

iconss:        ;图标显示片段
StringGetPos, pos, 1weather1, 转
if errorlevel = 0
{
	Length := StrLen(1weather1)
	Length := Length - pos
	pos ++
	StringTrimRight, daily, 1weather1, Length
	StringTrimleft, night, 1weather1, pos
	if ( a_hour >= 6 and a_hour <18)
	{
		IfExist, %A_ScriptDir%\theme\day%daily%.ico
		{
			Menu, Tray, Icon, %A_ScriptDir%\theme\day%daily%.ico
		}
		else
		{
			Menu, Tray, Icon, %A_ScriptDir%\theme\ques.ico
		}
	}
	else
	{
		IfExist, %A_ScriptDir%\theme\night%night%.ico
		{
			Menu, Tray, Icon, %A_ScriptDir%\theme\night%night%.ico
		}
		else
		{
			Menu, Tray, Icon, %A_ScriptDir%\theme\ques.ico
		}
	}
}
else          ;上面是天气里面有"转"的处理方法
{
	if ( a_hour >= 6 and a_hour <18)
	{
		IfExist, %A_ScriptDir%\theme\day%1weather1%.ico
		{
			Menu, Tray, Icon, %A_ScriptDir%\theme\day%1weather1%.ico
		}
		else
		{
			Menu, Tray, Icon, %A_ScriptDir%\theme\ques.ico
		}
	}
	else
	{
		IfExist, %A_ScriptDir%\theme\night%1weather1%.ico
		{
			Menu, Tray, Icon, %A_ScriptDir%\theme\night%1weather1%.ico
		}
		else
		{
			Menu, Tray, Icon, %A_ScriptDir%\theme\ques.ico
		}
	}
}
return

PowerBroadcast(wParam)  ;将0x218的wParam值作为此函数里的wParam变量
{
If (wParam = 7 OR wParam = 8) ;电源广播消息的wParam值如为7:挂起，8:待机
{
	gosub, mainapp
}
}

ConnectedToInternet(flag=0x40) {       ;获取网络状态函数
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}

exits:
ExitApp
return